<!DOCTYPE html>
<html lang=zh>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>js的异步编程、编译智能合约、Ether.js部署合约与发送交易 | Hexo</title>
  <meta name="description" content="Promise Pending Fulfilled rejected  函数示例1234567891011121314const getData &#x3D; () &#x3D;&gt; &amp;#123;  return Promise;&amp;#125;;const deploy &#x3D; () &#x3D;&gt; &amp;#123;&amp;#125;;&#x2F;&#x2F;若是要使用await必须是在async标识的函数中const main &#x3D; async ()">
<meta property="og:type" content="article">
<meta property="og:title" content="js的异步编程、编译智能合约、Ether.js部署合约与发送交易">
<meta property="og:url" content="https://imynniu.gitee.io/notes/2023/04/02/04-02-Lesson5-1/index.html">
<meta property="og:site_name" content="wStar">
<meta property="og:description" content="Promise Pending Fulfilled rejected  函数示例1234567891011121314const getData &#x3D; () &#x3D;&gt; &amp;#123;  return Promise;&amp;#125;;const deploy &#x3D; () &#x3D;&gt; &amp;#123;&amp;#125;;&#x2F;&#x2F;若是要使用await必须是在async标识的函数中const main &#x3D; async ()">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-04-01T16:00:00.000Z">
<meta property="article:modified_time" content="2023-04-02T09:55:16.753Z">
<meta property="article:author" content="Imynniu">
<meta property="article:tag" content="solidity">
<meta name="twitter:card" content="summary">
  <!-- Canonical links -->
  <link rel="canonical" href="https://imynniu.gitee.io/notes/2023/04/02/04-02-Lesson5-1/index.html">
  
    <link rel="alternate" href="/atom.xml" title="wStar" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  
<link rel="stylesheet" href="/notes/css/style.css">

  
  
  
    <link href="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.3.5/dist/jquery.fancybox.min.css" rel="stylesheet">
  
  
<meta name="generator" content="Hexo 6.3.0"></head>


<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://gitee.com/imynniu" target="_blank">
          <img class="img-circle img-rotate" src="/notes/images/avatar.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">Imynniu</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">Web3 enthusiast &amp; Student</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> DaLian, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/notes/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/notes/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/notes/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/notes/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-repository">
          <a href="/notes/repository">
            
            <i class="icon icon-project"></i>
            
            <span class="menu-title">项目</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/notes/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/imynniu" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p><br><a herf="https://imynniu.gitee.io/">点此返回主页<a/>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Other/">Other</a><span class="category-list-count">2</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Other/Git/">Git</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Other/MarkDown/">MarkDown</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Web3/">Web3</a><span class="category-list-count">3</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Web3/ether-js/">ether.js</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/Web3/solidity/">solidity</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/solidity/">solidity</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/%E5%8C%BA%E5%9D%97%E9%93%BE/">区块链</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/notes/categories/%E5%8C%BA%E5%9D%97%E9%93%BE/MEV/">MEV</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/notes/categories/%E8%8B%B1%E8%AF%AD/">英语</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/notes/categories/%E8%8B%B1%E8%AF%AD/%E8%8B%B1%E8%AF%AD%E9%9F%B3%E6%A0%87/">英语音标</a><span class="category-list-count">1</span></li></ul></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签</h3>
    <div class="widget-body">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/Git/" rel="tag">Git</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/MarkDown/" rel="tag">MarkDown</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/PBS/" rel="tag">PBS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/ethereum/" rel="tag">ethereum</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/fullStackWeb3/" rel="tag">fullStackWeb3</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/javaScript/" rel="tag">javaScript</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/solidity/" rel="tag">solidity</a><span class="tag-list-count">12</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/%E4%BB%A5%E5%A4%AA%E5%9D%8A/" rel="tag">以太坊</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/%E5%B7%A5%E5%85%B7/" rel="tag">工具</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/%E8%8B%B1%E8%AF%AD/" rel="tag">英语</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/notes/tags/%E8%8B%B1%E8%AF%AD%E9%9F%B3%E6%A0%87/" rel="tag">英语音标</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/notes/tags/Git/" style="font-size: 13px;">Git</a> <a href="/notes/tags/MarkDown/" style="font-size: 13px;">MarkDown</a> <a href="/notes/tags/PBS/" style="font-size: 13px;">PBS</a> <a href="/notes/tags/ethereum/" style="font-size: 13px;">ethereum</a> <a href="/notes/tags/fullStackWeb3/" style="font-size: 13.67px;">fullStackWeb3</a> <a href="/notes/tags/javaScript/" style="font-size: 13px;">javaScript</a> <a href="/notes/tags/solidity/" style="font-size: 14px;">solidity</a> <a href="/notes/tags/%E4%BB%A5%E5%A4%AA%E5%9D%8A/" style="font-size: 13px;">以太坊</a> <a href="/notes/tags/%E5%B7%A5%E5%85%B7/" style="font-size: 13.33px;">工具</a> <a href="/notes/tags/%E8%8B%B1%E8%AF%AD/" style="font-size: 13px;">英语</a> <a href="/notes/tags/%E8%8B%B1%E8%AF%AD%E9%9F%B3%E6%A0%87/" style="font-size: 13px;">英语音标</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/notes/archives/2023/04/">四月 2023</a><span class="archive-list-count">8</span></li><li class="archive-list-item"><a class="archive-list-link" href="/notes/archives/2023/03/">三月 2023</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/notes/archives/2022/11/">十一月 2022</a><span class="archive-list-count">3</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled ">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/notes/categories/solidity/">solidity</a>
              </p>
              <p class="item-title">
                <a href="/notes/2023/04/02/04-02-Lesson4/" class="title">solidity中的特殊函数</a>
              </p>
              <p class="item-date">
                <time datetime="2023-04-01T16:00:00.000Z" itemprop="datePublished">2023-04-02</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/notes/categories/solidity/">solidity</a>
              </p>
              <p class="item-title">
                <a href="/notes/2023/04/02/04-02-Lesson5-1/" class="title">js的异步编程、编译智能合约、Ether.js部署合约与发送交易</a>
              </p>
              <p class="item-date">
                <time datetime="2023-04-01T16:00:00.000Z" itemprop="datePublished">2023-04-02</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/notes/categories/solidity/">solidity</a>
              </p>
              <p class="item-title">
                <a href="/notes/2023/04/02/04-02-Lesson5-2/" class="title">js的异步编程、编译智能合约、Ether.js部署合约与发送交易</a>
              </p>
              <p class="item-date">
                <time datetime="2023-04-01T16:00:00.000Z" itemprop="datePublished">2023-04-02</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/notes/categories/solidity/">solidity</a>
              </p>
              <p class="item-title">
                <a href="/notes/2023/04/02/04-02-Lesson5/" class="title">wsl环境配置</a>
              </p>
              <p class="item-date">
                <time datetime="2023-04-01T16:00:00.000Z" itemprop="datePublished">2023-04-02</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/notes/categories/solidity/">solidity</a>
              </p>
              <p class="item-title">
                <a href="/notes/2023/04/01/04-01-Lesson3/" class="title">可组合性,合约之间的交互,继承和重写</a>
              </p>
              <p class="item-date">
                <time datetime="2023-03-31T16:00:00.000Z" itemprop="datePublished">2023-04-01</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-04-02-Lesson5-1" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      js的异步编程、编译智能合约、Ether.js部署合约与发送交易
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/notes/2023/04/02/04-02-Lesson5-1/" class="article-date">
	  <time datetime="2023-04-01T16:00:00.000Z" itemprop="datePublished">2023-04-02</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/notes/categories/solidity/">solidity</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link-link" href="/notes/tags/solidity/" rel="tag">solidity</a>
  </span>


        
	<span class="article-read hidden-xs">
	    <i class="icon icon-eye-fill" aria-hidden="true"></i>
	    <span id="busuanzi_container_page_pv">
			<span id="busuanzi_value_page_pv">0</span>
		</span>
	</span>


        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/notes/2023/04/02/04-02-Lesson5-1/#comments" class="article-comment-link">评论</a></span>
        
	
		<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 486(字)</span>
	
	
		<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 2(分)</span>
	

      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <h3 id="Promise"><a href="#Promise" class="headerlink" title="Promise"></a>Promise</h3><ul>
<li>Pending</li>
<li>Fulfilled</li>
<li>rejected</li>
</ul>
<h4 id="函数示例"><a href="#函数示例" class="headerlink" title="函数示例"></a>函数示例</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">getData</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="title class_">Promise</span>;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">deploy</span> = (<span class="params"></span>) =&gt; &#123;&#125;;</span><br><span class="line"><span class="comment">//若是要使用await必须是在async标识的函数中</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">main</span> = <span class="keyword">async</span> (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">let</span> status = <span class="keyword">await</span> <span class="title function_">getData</span>();</span><br><span class="line">  <span class="keyword">await</span> <span class="title function_">deploy</span>();</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//async函数的调用方式</span></span><br><span class="line"><span class="title function_">main</span>()</span><br><span class="line">  .<span class="title function_">then</span>()</span><br><span class="line">  .<span class="title function_">catch</span>(<span class="function">(<span class="params">err</span>) =&gt;</span> <span class="variable language_">console</span>.<span class="title function_">log</span>(err));</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="编译智能合约"><a href="#编译智能合约" class="headerlink" title="编译智能合约"></a>编译智能合约</h3><ul>
<li><p>安装solc-js，<a target="_blank" rel="noopener" href="https://github.com/ethereum/solc-js">github地址</a></p>
<p>  命令行全局安装，就可以识别solcjs</p>
  <figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g solc</span><br></pre></td></tr></table></figure>
</li>
<li><p>脚本</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">solcjs --bin --abi --include-path node_modules/ --base-path . -o . SimpleStorage.sol</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="fake-Blockchain"><a href="#fake-Blockchain" class="headerlink" title="fake Blockchain"></a>fake Blockchain</h3><ul>
<li><a target="_blank" rel="noopener" href="https://trufflesuite.com/ganache/">Ganache</a></li>
</ul>
<h3 id="Ether-js"><a href="#Ether-js" class="headerlink" title="Ether.js"></a>Ether.js</h3><p><a target="_blank" rel="noopener" href="https://docs.ethers.org/v5/getting-started/">文档</a></p>
<ul>
<li><p>可以与不同的区块链进行交互</p>
</li>
<li><p>另一个与之比较相似的是<a target="_blank" rel="noopener" href="https://web3js.readthedocs.io/en/v1.8.2/">web3.js</a></p>
</li>
</ul>
<h4 id="部署流程"><a href="#部署流程" class="headerlink" title="部署流程"></a>部署流程</h4><ol>
<li><p>使用ethers连接远程链</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> provider = <span class="keyword">new</span> ethers.<span class="property">providers</span>.<span class="title class_">JsonRpcProvider</span>(</span><br><span class="line">  <span class="string">&quot;HTTP://172.24.224.1:7545&quot;</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
</li>
<li><p>连接钱包</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> wallet = <span class="keyword">new</span> ethers.<span class="title class_">Wallet</span>(</span><br><span class="line">  <span class="string">&quot;0x576c26b75ff2154f46ca13ebaa56572ae49aa99cc48b323d26a794128be12029&quot;</span>,</span><br><span class="line">  provider</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
</li>
<li><p>获取abi和binary</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> abi = fs.<span class="title function_">readFileSync</span>(<span class="string">&quot;./SimpleStorage_sol_SimpleStorage.abi&quot;</span>, <span class="string">&quot;utf-8&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> binary = fs.<span class="title function_">readFileSync</span>(</span><br><span class="line">  <span class="string">&quot;./SimpleStorage_sol_SimpleStorage.bin&quot;</span>,</span><br><span class="line">  <span class="string">&quot;utf-8&quot;</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
</li>
<li><p>构建合约</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> contractFactory = <span class="keyword">new</span> ethers.<span class="title class_">ContractFactory</span>(abi, binary, wallet);</span><br></pre></td></tr></table></figure>
</li>
<li><p>部署合约</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;正在部署合约...&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> contract = <span class="keyword">await</span> contractFactory.<span class="title function_">deploy</span>();</span><br></pre></td></tr></table></figure>
</li>
<li><p>交易收据</p>
<ul>
<li>在第五个区块后确认交易的交易收据</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> tarnsactionReceipt = <span class="keyword">await</span> contract.<span class="property">deployTransaction</span>.<span class="title function_">wait</span>(<span class="number">5</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(tarnsactionReceipt);</span><br></pre></td></tr></table></figure>
</li>
<li><p>完整代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; ethers &#125; = <span class="built_in">require</span>(<span class="string">&quot;ethers&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&quot;fs-extra&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">main</span> = <span class="keyword">async</span> (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">  <span class="comment">//HTTP://172.24.224.1:7545</span></span><br><span class="line">  <span class="keyword">const</span> provider = <span class="keyword">new</span> ethers.<span class="property">providers</span>.<span class="title class_">JsonRpcProvider</span>(</span><br><span class="line">    <span class="string">&quot;HTTP://172.24.224.1:7545&quot;</span></span><br><span class="line">  );</span><br><span class="line">  <span class="keyword">const</span> wallet = <span class="keyword">new</span> ethers.<span class="title class_">Wallet</span>(</span><br><span class="line">    <span class="string">&quot;0x576c26b75ff2154f46ca13ebaa56572ae49aa99cc48b323d26a794128be12029&quot;</span>,</span><br><span class="line">    provider</span><br><span class="line">  );</span><br><span class="line">  <span class="keyword">const</span> abi = fs.<span class="title function_">readFileSync</span>(<span class="string">&quot;./SimpleStorage_sol_SimpleStorage.abi&quot;</span>, <span class="string">&quot;utf-8&quot;</span>);</span><br><span class="line">  <span class="keyword">const</span> binary = fs.<span class="title function_">readFileSync</span>(</span><br><span class="line">    <span class="string">&quot;./SimpleStorage_sol_SimpleStorage.bin&quot;</span>,</span><br><span class="line">    <span class="string">&quot;utf-8&quot;</span></span><br><span class="line">  );</span><br><span class="line">  <span class="keyword">const</span> contractFactory = <span class="keyword">new</span> ethers.<span class="title class_">ContractFactory</span>(abi, binary, wallet);</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;正在部署合约...&quot;</span>);</span><br><span class="line">  <span class="keyword">const</span> contract = <span class="keyword">await</span> contractFactory.<span class="title function_">deploy</span>();</span><br><span class="line">  <span class="keyword">const</span> tarnsactionReceipt = <span class="keyword">await</span> contract.<span class="property">deployTransaction</span>.<span class="title function_">wait</span>(<span class="number">5</span>);</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(tarnsactionReceipt);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="title function_">main</span>()</span><br><span class="line">  .<span class="title function_">then</span>()</span><br><span class="line">  .<span class="title function_">catch</span>(<span class="function">(<span class="params">err</span>) =&gt;</span> <span class="variable language_">console</span>.<span class="title function_">log</span>(err));</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="发送原生交易"><a href="#发送原生交易" class="headerlink" title="发送原生交易"></a>发送原生交易</h4><ol>
<li><p>创建交易对象</p>
<ul>
<li><p>获取当前地址的交易nonce</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> nonce = <span class="keyword">await</span> wallet.<span class="title function_">getTransactionCount</span>();</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建交易对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> tx = &#123;</span><br><span class="line">  <span class="attr">nonce</span>: nonce,</span><br><span class="line">  <span class="attr">gasPrice</span>: <span class="number">20000000000</span>,</span><br><span class="line">  <span class="attr">gasLimit</span>: <span class="number">1000000</span>,</span><br><span class="line">  <span class="attr">to</span>: <span class="literal">null</span>,</span><br><span class="line">  <span class="attr">value</span>: <span class="number">0</span>,</span><br><span class="line">  <span class="attr">data</span>: <span class="string">&quot;0xbinary&quot;</span>,</span><br><span class="line">  <span class="attr">chainId</span>: <span class="number">1337</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p><del>交易签名</del></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> signedTxResponse = <span class="keyword">await</span> wallet.<span class="title function_">signTransaction</span>(tx);</span><br></pre></td></tr></table></figure>
</li>
<li><p>发送交易</p>
<ul>
<li>使用sendTransaction方法不用对交易签名（2的操作）</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//发送交易</span></span><br><span class="line"><span class="keyword">const</span> sentTxResponse = <span class="keyword">await</span> wallet.<span class="title function_">sendTransaction</span>(tx);</span><br><span class="line"><span class="keyword">await</span> sentTxResponse.<span class="title function_">wait</span>(<span class="number">1</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(sentTxResponse);</span><br></pre></td></tr></table></figure></li>
</ol>

      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="https://imynniu.gitee.io/notes/2023/04/02/04-02-Lesson5-1/" title="js的异步编程、编译智能合约、Ether.js部署合约与发送交易" target="_blank" rel="external">https://imynniu.gitee.io/notes/2023/04/02/04-02-Lesson5-1/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://gitee.com/imynniu" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/notes/images/avatar.jpg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://gitee.com/imynniu" target="_blank"><span class="text-dark">Imynniu</span><small class="ml-1x">Web3 enthusiast &amp; Student</small></a></h3>
        <div>Web3游民。</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/notes/2023/04/02/04-02-Lesson4/" title="solidity中的特殊函数"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/notes/2023/04/02/04-02-Lesson5-2/" title="js的异步编程、编译智能合约、Ether.js部署合约与发送交易"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
  </ul>
  
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  


</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/imynniu" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>

<script src="/notes/js/plugin.min.js"></script>


<script src="/notes/js/application.js"></script>


    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/notes/',
        CONTENT_URL: '/notes/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/notes/js/insight.js"></script>






   
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





   


  <script src="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.3.5/dist/jquery.fancybox.min.js"></script>
  <script>
  //利用 FancyBox 实现点击图片放大
  $(document).ready(function() {
    $('article img').not('[hidden]').not('.panel-body img').each(function() {
      var $image = $(this);
      var imageCaption = $image.attr('alt');
      var $imageWrapLink = $image.parent('a');
      if ($imageWrapLink.length < 1) {
        var src = this.getAttribute('src');
        var idx = src.lastIndexOf('?');
        if (idx != -1) {
          src = src.substring(0, idx);
        }
        $imageWrapLink = $image.wrap('<a href="' + src + '"></a>').parent('a');
      }
      $imageWrapLink.attr('data-fancybox', 'images');
      if (imageCaption) {
        $imageWrapLink.attr('data-caption', imageCaption);
      }
    });
    $().fancybox({
      selector: '[data-fancybox="images"]',
      hash: false,
      loop: false,
    });
  });
  </script>





</body>
</html>